import com.alibaba.fastjson.JSON;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/Provinces")
public class ProvincesServlet extends HttpServlet {
    @Override
    public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
        res.setContentType("application/json;charset=utf-8");
        //创建连接池
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        //创建sql执行器
        QueryRunner queryRunner = new QueryRunner(dataSource);
        String sql = " select * from province ";
        try {
            List<Province> provinces = queryRunner.query(sql, new BeanListHandler<Province>(Province.class));
            String s = JSON.toJSONString(provinces);
            System.out.println(s);
            res.getWriter().print(s);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
